/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.raip.io
 *
 * 版权所有，侵权必究！
 */

package com.cz.modules.job.task;

import com.cz.common.utils.ParkingUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import nova.traffic.NovaTraffic;
import nova.traffic.been.DeviceMemorySize;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 测试定时任务(演示Demo，可删除)
 *
 * testTask为spring bean的名称
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component("weatherForecastTask")
public class WeatherForecastTask implements ITask {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void run(String params){
		logger.debug("TestTask定时任务正在执行，参数为：{}", params);

		String url = "https://api.seniverse.com/v3/weather/daily.json?key=SqgBoqgD2Xz1mPnG6&location=Taiyuan&language=zh-Hans&unit=c&start=0&days=2";
		String resultGet = doHttpGet(url);
		jiexi(resultGet);
	}
	public static String doHttpGet(String url){
		String result = null;
		//1.获取httpclient
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//接口返回结果
		CloseableHttpResponse response = null;
		String paramStr = null;
		try {
			//paramStr = EntityUtils.toString(new UrlEncodedFormEntity(params));
			//拼接参数
			StringBuffer sb = new StringBuffer();
			sb.append(url);
			//sb.append("?");
			//sb.append(paramStr);
			//2.创建get请求
			HttpGet httpGet = new HttpGet(sb.toString());
			//3.设置请求和传输超时时间
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
			httpGet.setConfig(requestConfig);
            /*此处可以添加一些请求头信息，例如：
            httpGet.addHeader("content-type","text/xml");*/
			//4.提交参数
			response = httpClient.execute(httpGet);
			//5.得到响应信息
			int statusCode = response.getStatusLine().getStatusCode();
			//6.判断响应信息是否正确
			if(HttpStatus.SC_OK != statusCode){
				//终止并抛出异常
				httpGet.abort();
				throw new RuntimeException("HttpClient,error status code :" + statusCode);
			}
			//7.转换成实体类
			HttpEntity entity = response.getEntity();
			if(null != entity){
				result = EntityUtils.toString(entity);
			}
			EntityUtils.consume(entity);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//8.关闭所有资源连接
			if(null != response){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null != httpClient){
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	public static void jiexi(String result){
		if(result != null){
			JSONObject json_test = JSONObject.fromObject(result);
			JSONArray results = (JSONArray) json_test.get("results");
			JSONObject data = results.getJSONObject(0);
			JSONArray daily = (JSONArray) data.get("daily");
			String highTemp = daily.getJSONObject(0).get("high")+"℃";
			String lowTemp = daily.getJSONObject(0).get("low")+"℃";
			String typeDay = (String) daily.getJSONObject(0).get("text_day");
			String typeNight = (String) daily.getJSONObject(0).get("text_night");
			/*if(typeDay != null){
				if(typeDay.equals(typeNight)){
					ParkingUtil.tempType = typeDay;
				}else{
					ParkingUtil.tempType = typeDay+"转"+typeNight;
				}
			}*/
			ParkingUtil.tempType = typeDay;
			ParkingUtil.highTemp = highTemp;
			ParkingUtil.lowTemp = lowTemp;
		}
	}
}
